package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        // 1.查询菜品分页数据
        // 1-1 查询条件封装
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(name), Dish::getName, name);
        // 1-2 分页条件封装
        Page<Dish> page = new Page<>(pageNum, pageSize);
        // 1-3 执行mapper查询
        page = dishMapper.selectPage(page, wrapper);
        // 2.遍历每一个菜品对象
        List<Dish> dishList = page.getRecords();
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                // 3.查询分类对象
                Category category = categoryMapper.selectById(dish.getCategoryId());
                dish.setCategoryName(category.getName());

                // 4.查询口味列表
                // 4-1 封装口味的查询条件
                LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
                dishFlavorWrapper.eq(DishFlavor::getDishId, dish.getId());
                // 4-2 查询list
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dishFlavorWrapper);
                // 4-3 封装到菜品对象中
                dish.setFlavors(dishFlavorList);
            }
        }

        return page; // 菜品（分类、口味）
    }

    @Override
    public void save(Dish dish) {
        // 1.保存菜品的基本信息
        log.info("菜品保存之前：{}", dish.getId());
        dishMapper.insert(dish);
        log.info("菜品保存之后：{}", dish.getId());

        // 2.保存菜品的口味
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                // 指定菜品id
                dishFlavor.setDishId(dish.getId());
                // 保存
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public Dish findById(Long id) {
        //先查看菜品基本信息
        Dish dish = dishMapper.selectById(id);
        //2.再查询口味列表
        //2.1构建口味的查询条件对象
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, id);
        //2.2查询列表
        List<DishFlavor> flavorList = dishFlavorMapper.selectList(wrapper);
        //3.将口味列表设置到菜品中
        dish.setFlavors(flavorList);
        //4.返回菜品对象
        return dish;
    }
//菜品修改
    @Override
    public void update(Dish dish) {
        //1.先更新菜品信息
        dishMapper.updateById(dish);
        //2.删除原有口味
        //构建口味条件对象
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        //2.2执行mapper删除
        dishFlavorMapper.delete(wrapper);
        //3.遍历前端提交的口味
        List<DishFlavor> flavorList = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {

                //设置菜品id
                dishFlavor.setDishId(dish.getId());
                //调用mapper保存口味
                dishFlavorMapper.insert(dishFlavor);
            }

        }
    }
//删除菜品
    @Override
    public void deleteBatchIds(List<Long> ids) {
        //先删菜品
        dishMapper.deleteBatchIds(ids);
        //再删口味
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(DishFlavor::getDishId, ids);
        dishFlavorMapper.delete(wrapper);
    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //条件对象
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId, ids);
        //修改实体
        Dish dish = new Dish();
        dish.setStatus(status);
        //调用mapper
        dishMapper.update(dish, wrapper);
    }
//根据分类id 查询菜品列表
    @Override
    public List<Dish> findByCategoryId(Long categoryId) {
        //1.构建条件
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId);//category_id=xx
        wrapper.eq(Dish::getStatus, 1);//statuas=1
        //2.查询list
        return dishMapper.selectList(wrapper);

    }
    //根据分类id和type=1查询菜品列表
    @Override
    public List<Dish> findList(Long categoryId, Integer status, String name) {
        //设置条件查询
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId != null, Dish::getCategoryId, categoryId);
        wrapper.eq(Dish::getStatus, status);
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        //查询菜品列表
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //遍历列表
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //得到每个菜品,根据菜品的id从口味表找到当前菜品的口味列表
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavorList);
            }
        }
        return dishList;

    }
//H5菜品显示
    @Override
    public List<Dish> findDishLish(Long categoryId, Integer status) {
        //查询菜品列表
        //1.1构建条件
        LambdaQueryWrapper<Dish> dishwrapper = new LambdaQueryWrapper<>();
        dishwrapper.eq(Dish::getCategoryId, categoryId);
        dishwrapper.eq(Dish::getStatus, status);
        //1.2查询
        List<Dish> dishList = dishMapper.selectList(dishwrapper);
        //遍历菜品
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //查询菜品对应口味列表
                //构建条件
                LambdaQueryWrapper<DishFlavor> flavorwrapper = new LambdaQueryWrapper<>();
                flavorwrapper.eq(DishFlavor::getDishId, dish.getId());
                //查询口味
                List<DishFlavor> flavorList = dishFlavorMapper.selectList(flavorwrapper);
                //设置菜品
                dish.setFlavors(flavorList);

            }
        }
        //返回结果
        return dishList;


    }


}